package templates;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DivideAndCompute {
    public static void main(String[] args) {
        String str = "11+11";
        String[] strings = mySplit(str);
        System.out.println(diffWaysToCompute(strings).toString());

    }

    public static List<Integer> diffWaysToCompute(String[] input) {
        List<Integer> resList = new ArrayList<>();
        if (input.length < 1){
            return resList;
        }
        // 停止条件
        if (input.length <= 1){
            resList.add(Integer.valueOf(input[0]));
            return resList;
        }
        // 循环状态
        for (int i = 1;i < input.length ;){
            List<Integer> leftRes = diffWaysToCompute(Arrays.copyOfRange(input, 0,i));
            List<Integer> rightRes = diffWaysToCompute(Arrays.copyOfRange(input, i + 1,input.length));

            String signal = input[i];
            for (Integer left: leftRes) {
                for (Integer right: rightRes) {
                    resList.add(compute(left,right,signal));
                }
            }
            i += 2;
        }
        return resList;

    }

    private static String[] mySplit(String input) {
        List<String> strings = new ArrayList<>();
        int startIndex = 0;
        for (int i = 0; i <= input.length(); i++) {
            if (i == input.length() || input.charAt(i) == '+' || input.charAt(i) == '-'|| input.charAt(i) == '*'){
                strings.add(input.substring(startIndex,i));
                if (i != input.length()){
                    strings.add(input.substring(i,i + 1));
                }
                startIndex = i + 1;
            }
        }
        String[] resStrs = new String[strings.size()];
        for (int i = 0; i < strings.size(); i++) {
            resStrs[i] = strings.get(i);
        }
        return resStrs;
    }

    private static int compute(int left, int right, String signal) {
        if (signal.equals("+")){
            return left + right;
        }else if (signal.equals("-")){
            return left - right;
        }else if (signal.equals("*")){
            return left * right;
        }else {
            return left/right;
        }
    }
}
